{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RAG with Excel Spreadsheet using LlamaPrase\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_parse/blob/main/examples/demo_excel.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "This notebook shows you using LlamaParse with Excel Spreadsheet.\n",
    "\n",
    "We will use NVIDIA revenue [data](https://investor.nvidia.com/financial-info/quarterly-results/default.aspx) from last 5 quarters\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index\n",
    "%pip install llama-parse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set LLAMA_CLOUD_API_KEY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()\n",
    "\n",
    "from llama_parse import LlamaParse\n",
    "\n",
    "api_key = \"llx-\"  # get from cloud.llamaindex.ai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Use LlamaParse to parse excel document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started parsing the file under job_id 94b4d648-1e3f-43d9-9065-20893f2d5822\n"
     ]
    }
   ],
   "source": [
    "parser = LlamaParse(\n",
    "    api_key=api_key,  # can also be set in your env as LLAMA_CLOUD_API_KEY\n",
    "    result_type=\"markdown\",\n",
    ")\n",
    "\n",
    "documents = parser.load_data(\"../data/nvidia_quarterly_revenue_trend_by_market.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(id_='93202576-4284-4473-80b3-8c9b31c93088', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, text='|                          |NVIDIA QUARTERLY REVENUE TREND REVENUE BY MARKET|       |       |       |       |       |       |       |\\n|--------------------------|------------------------------------------------|-------|-------|-------|-------|-------|-------|-------|\\n|                          |                                                |       |       |       |       |       |       |       |\\n|($ in millions)           |Q1 FY25                                         |Q4 FY24|Q3 FY24|Q2 FY24|Q1 FY24|Q4 FY23|Q3 FY23|Q2 FY23|\\n|Data Center               |$22,563                                         |$18,404|$14,514|$10,323|$4,284 |$3,616 |$3,833 |$3,806 |\\n|Gaming                    |2647                                            |2865   |2856   |2486   |2240   |1831   |1574   |2042   |\\n|Professional Visualization|427                                             |463    |416    |379    |295    |226    |200    |496    |\\n|Auto                      |329                                             |281    |261    |253    |296    |294    |251    |220    |\\n|OEM & Other               |78                                              |90     |73     |66     |77     |84     |73     |140    |\\n|TOTAL                     |$26,044                                         |$22,103|$18,120|$13,507|$7,192 |$6,051 |$5,931 |$6,704 |\\n|                          |                                                |       |       |       |       |       |       |       |\\n|($ in millions)           |Q1 FY24                                         |Q4 FY23|Q3 FY23|Q2 FY23|Q1 FY23|Q4 FY22|Q3 FY22|Q2 FY22|\\n|Data Center               |$4,284                                          |$3,616 |$3,833 |$3,806 |$3,750 |$3,263 |$2,936 |$2,366 |\\n|Gaming                    |$2,240                                          |$1,831 |$1,574 |$2,042 |$3,620 |$3,420 |$3,221 |$3,061 |\\n|Professional Visualization|$295                                            |$226   |$200   |$496   |$622   |$643   |$577   |$519   |\\n|Auto                      |$296                                            |$294   |$251   |$220   |$138   |$125   |$135   |$152   |\\n|OEM & Other               |$77                                             |$84    |$73    |$140   |$158   |$192   |$234   |$409   |\\n|TOTAL                     |$7,192                                          |$6,051 |$5,931 |$6,704 |$8,288 |$7,643 |$7,103 |$6,507 |\\n|                          |                                                |       |       |       |       |       |       |       |\\n|($ in millions)           |Q4 FY24                                         |Q3 FY24|Q2 FY24|Q1 FY24|Q4 FY23|Q3 FY23|Q2 FY23|Q1 FY23|\\n|Data Center               |$18,404                                         |$14,514|$10,323|$4,284 |$3,616 |$3,833 |$3,806 |$3,750 |\\n|Gaming                    |$2,865                                          |$2,856 |$2,486 |$2,240 |$1,831 |$1,574 |$2,042 |$3,620 |\\n|Professional Visualization|$463                                            |$416   |$379   |$295   |$226   |$200   |$496   |$622   |\\n|Auto                      |$281                                            |$261   |$253   |$296   |$294   |$251   |$220   |$138   |\\n|OEM & Other               |$90                                             |$73    |$66    |$77    |$84    |$73    |$140   |$158   |\\n|TOTAL                     |$22,103                                         |$18,120|$13,507|$7,192 |$6,051 |$5,931 |$6,704 |$8,288 |\\n|                          |                                                |       |       |       |       |       |       |       |\\n|($ in millions)           |Q3 FY24                                         |Q2 FY24|Q1 FY24|Q4 FY23|Q3 FY23|Q2 FY23|Q1 FY23|Q4 FY22|\\n|Data Center               |$14,514                                         |$10,323|$4,284 |$3,616 |$3,833 |$3,806 |$3,750 |$3,263 |\\n|Gaming                    |$2,856                                          |$2,486 |$2,240 |$1,831 |$1,574 |$2,042 |$3,620 |$3,420 |\\n|Professional Visualization|$416                                            |$379   |$295   |$226   |$200   |$496   |$622   |$643   |\\n|Auto                      |$261                                            |$253   |$296   |$294   |$251   |$220   |$138   |$125   |\\n|OEM & Other               |$73                                             |$66    |$77    |$84    |$73    |$140   |$158   |$192   |\\n|TOTAL                     |$18,120                                         |$13,507|$7,192 |$6,051 |$5,931 |$6,704 |$8,288 |$7,643 |\\n|                          |                                                |       |       |       |       |       |       |       |\\n|($ in millions)           |Q2 FY24                                         |Q1 FY24|Q4 FY23|Q3 FY23|Q2 FY23|Q1 FY23|Q4 FY22|Q3 FY22|\\n|Data Center               |$10,323                                         |$4,284 |$3,616 |$3,833 |$3,806 |$3,750 |$3,263 |$2,936 |\\n|Gaming                    |$2,486                                          |$2,240 |$1,831 |$1,574 |$2,042 |$3,620 |$3,420 |$3,221 |\\n|Professional Visualization|$379                                            |$295   |$226   |$200   |$496   |$622   |$643   |$577   |\\n|Auto                      |$253                                            |$296   |$294   |$251   |$220   |$138   |$125   |$135   |\\n|OEM & Other               |$66                                             |$77    |$84    |$73    |$140   |$158   |$192   |$234   |\\n|TOTAL                     |$13,507                                         |$7,192 |$6,051 |$5,931 |$6,704 |$8,288 |$7,643 |$7,103 |\\n', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n')]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "documents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Set OpenAI API Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-\"\n",
    "\n",
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.core import Settings\n",
    "\n",
    "llm = OpenAI(model=\"gpt-4\")\n",
    "Settings.llm = llm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Build Index and QueryEngine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import VectorStoreIndex\n",
    "\n",
    "index = VectorStoreIndex.from_documents(documents)\n",
    "\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Querying"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total revenue in Q1 FY25 is $26,044 million.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\"What is the total revenue in Q1 FY25?\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The revenue for the Data Center sector increased from $3,750 million in Q1 FY23 to $22,563 million in Q1 FY25.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\n",
    "    \"What is the revenue growth of data centre from Q1 FY23 to Q1 FY25?\"\n",
    ")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The revenue of gaming in Q4 2024 was $2,865 million.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\"What is the revenue of gaming in Q4 2024?\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total revenue in Q4 FY24 was $22,103 million.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\"What is the total revenue in Q4 FY24?\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The revenue from Professional Visualization in the last four quarters of 2024 were as follows: Q1 FY24 - $295 million, Q2 FY24 - $379 million, Q3 FY24 - $416 million, and Q4 FY24 - $463 million.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\n",
    "    \"What is the revenue Professional Visualization in last 4 quarters of 2024?\"\n",
    ")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total revenue in Q3 FY24 was $18,120 million.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\"What is the total revenue in Q3 FY24?\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The revenue of the data center grew from $14,514 million in Q3 FY24 to $18,404 million in Q4 FY24.\n"
     ]
    }
   ],
   "source": [
    "response = query_engine.query(\n",
    "    \"What is the revenue growth of data centre from Q3 FY24 to Q4 FY24?\"\n",
    ")\n",
    "print(str(response))"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
